<template>
  <div>
    <div
      slot="header"
      class="clearfix"
    >
      <el-page-header
        class="title-row"
        content="告警发送"
        @back="back"
      />
      <br>
    </div>
    <div class="crud-group">
      <el-input
        v-model="paramsForm.cmName"
        placeholder="请输入来源类型"
        clearable
      />
      <el-input
        v-model="paramsForm.clusterName"
        placeholder="请输入发布类型"
        clearable
      />
      <el-input
        v-model="paramsForm.clusterName"
        placeholder="请输入发布状态"
        clearable
      />
      <div>
        <el-button
          icon="el-icon-search"
          type="success"
          @click="getData"
        >搜索</el-button>
        <el-button
          type="primary"
          icon="el-icon-plus"
          @click="addDialogVisible=true"
        >新增</el-button>
      </div>
    </div>
    <el-table
      ref="table"
      v-loading="isLoading"
      :data="data"
      style="margin-top:10px"
    >
      <el-table-column
        type="index"
        label="序号"
        width="55"
      />
      <el-table-column
        label="来源类型"
        prop="sourceType"
      />
      <el-table-column
        label="发送类型"
        prop="sendingType"
      >
        <template slot-scope="scope">
          <span v-if="scope.row.sendingType=='1'">微信</span>
          <span v-if="scope.row.sendingType=='2'">钉钉</span>
          <span v-if="scope.row.sendingType=='3'">微信,钉钉</span>
        </template>
      </el-table-column>
      <el-table-column
        label="接收人"
        prop="recipientUserName"
        show-overflow-tooltip
      />
      <el-table-column
        label="发布状态"
        prop="releaseType"
      >
        <template slot-scope="scope">
          <span v-if="scope.row.releaseType=='1'">发布</span>
          <span v-if="scope.row.releaseType=='2'">待发布</span>
        </template>
      </el-table-column>
      <el-table-column
        label="创建时间"
        prop="createTime"
        show-overflow-tooltip
      />
      <el-table-column
        label="发布主题"
        prop="publishTheme"
        show-overflow-tooltip
      />
      <el-table-column label="操作">
        <template slot-scope="scope">
          <el-button
            v-if="scope.row.releaseType=='2'"
            size="mini"
            type="text"
            @click="publish(scope.row)"
          >发布</el-button>
          <el-button
            v-if="scope.row.releaseType=='1'"
            size="mini"
            type="text"
            @click="unpublish(scope.row)"
          >待发布</el-button>
          <el-button
            size="mini"
            type="text"
            @click="editData(scope.row)"
          >修改</el-button>
          <el-button
            size="mini"
            type="text"
            @click="delData(scope.row)"
          >删除</el-button>
        </template>
      </el-table-column>
    </el-table>
    <div class="pagination-container">
      <el-pagination
        :current-page="paramsForm.current"
        :page-size="paramsForm.size"
        :page-sizes="[5, 10, 20, 30, 40]"
        layout="prev, pager, next,sizes,total"
        :total="total"
        @size-change="handleSizeChange"
        @current-change="handleCurrentChange"
      />
    </div>

    <!-- 新增Dialog -->
    <el-dialog
      :close-on-click-modal="false"
      :before-close="handleClose"
      :visible.sync="addDialogVisible"
      title="新增"
      width="800px"
    >
      <el-form
        ref="form"
        :inline="true"
        label-width="120px"
        :rules="rules"
        :model="addForm"
      >
        <el-form-item label="来源类型:">
          <el-select
            v-model="addForm.sourceType"
            placeholder="请选择类型"
            style="width:190px"
            @change="getTopic"
          >
            <el-option
              v-for="val in sourceList"
              :key="val"
              :label="val"
              :value="val"
            />
          </el-select>
        </el-form-item>
        <el-form-item label="发送类型:">
          <el-select
            v-model="addForm.sendingType"
            placeholder="请选择类型"
            style="width:190px"
          >
            <el-option
              v-for="val in sendList"
              :key="val.value"
              :label="val.label"
              :value="val.value"
            />
          </el-select>
        </el-form-item>
        <el-form-item label="发布状态:">
          <el-select
            v-model="addForm.releaseType"
            placeholder="请选择状态"
            style="width:190px"
            disabled
          >
            <el-option
              v-for="val in statusList"
              :key="val"
              :label="val.label"
              :value="val.value"
            />
          </el-select>
        </el-form-item>
        <el-form-item label="发布主题:">
          <el-select
            v-model="addForm.publishTheme"
            placeholder="请选择主题"
            style="width:190px"
          >
            <el-option
              v-for="val in topicList"
              :key="val"
              :label="val"
              :value="val"
            />
          </el-select>
        </el-form-item>
        <el-form-item
          label="接收人名称:"
          prop="warningUserModels"
        >
          <el-select
            v-model="addForm.warningUserModels"
            placeholder="请选择名称"
            multiple
            style="width:190px"
          >
            <el-option
              v-for="val in nameList"
              :key="val.value"
              :label="val.label"
              :value="val.value"
            />
          </el-select>
        </el-form-item>
      </el-form>
      <div
        slot="footer"
        class="dialog-footer"
      >
        <el-button
          type="primary"
          @click="handleClose"
        >取 消</el-button>
        <el-button
          type="primary"
          @click="addSend"
        >确 定</el-button>
      </div>
    </el-dialog>
    <!-- 编辑Dialog -->
    <el-dialog
      :close-on-click-modal="false"
      :before-close="editClose"
      :visible.sync="editFlag"
      title="修改"
      width="800px"
    >
      <el-form
        ref="forms"
        :inline="true"
        label-width="120px"
        :rules="rules"
        :model="editForm"
      >
        <el-form-item label="来源类型:">
          <el-select
            v-model="editForm.sourceType"
            placeholder="请选择类型"
            style="width:190px"
          >
            <el-option
              v-for="val in sourceList"
              :key="val"
              :label="val"
              :value="val"
            />
          </el-select>
        </el-form-item>
        <el-form-item label="发送类型:">
          <el-select
            v-model="editForm.sendingType"
            placeholder="请选择类型"
            style="width:190px"
          >
            <el-option
              v-for="val in sendList"
              :key="val.value"
              :label="val.label"
              :value="val.value"
            />
          </el-select>
        </el-form-item>
        <el-form-item label="发布状态:">
          <el-select
            v-model="editForm.releaseType"
            placeholder="请选择状态"
            style="width:190px"
            disabled
          >
            <el-option
              v-for="val in statusList"
              :key="val.value"
              :label="val.label"
              :value="val.value"
            />
          </el-select>
        </el-form-item>
        <el-form-item label="发布主题:">
          <el-select
            v-model="editForm.publishTheme"
            placeholder="请选择主题"
            style="width:190px"
          >
            <el-option
              v-for="val in topicList"
              :key="val"
              :label="val"
              :value="val"
            />
          </el-select>
        </el-form-item>
        <el-form-item
          label="接收人名称:"
          prop="warningUserModels"
        >
          <el-select
            v-model="editForm.warningUserModels"
            placeholder="请选择名称"
            multiple
            style="width:190px"
          >
            <el-option
              v-for="val in nameList"
              :key="val.value"
              :label="val.label"
              :value="val.value"
            />
          </el-select>
        </el-form-item>
      </el-form>
      <div
        slot="footer"
        class="dialog-footer"
      >
        <el-button
          type="primary"
          @click="editClose"
        >取 消</el-button>
        <el-button
          type="primary"
          @click="editSend"
        >确 定</el-button>
      </div>
    </el-dialog>
  </div>
</template>
<script>
import parameter from '@/api/phaseTwo/parameter.js'

export default {
  data() {
    return {
      total: 0,
      paramsForm: {
        current: 1,
        size: 10
      },
      data: [],
      isLoading: false,
      permission: {
        add: ['admin', 'add'],
        edit: ['edit'],
        del: ['del']
      },
      addDialogVisible: false,
      tableList: [
        {
          source: 'hive',
          send: '钉钉',
          receiver: '张三',
          status: '已发布',
          createTime: '2023-6-14 14:17:21',
          topic: 'hive的JVM过高'
        },
        {
          source: 'impala',
          send: '企业微信',
          receiver: '李四',
          status: '已发布',
          createTime: '2023-6-14 14:06:21',
          topic: 'impala的JVM过高'
        },
        {
          source: 'yarn',
          send: '企业微信',
          receiver: '王五',
          status: '待发布',
          createTime: '2023-6-14 13:54:21',
          topic: 'yarn的JVM过高'
        }
      ],
      addForm: {
        sourceType: '',
        sendingType: '',
        releaseType: '2',
        publishTheme: '',
        warningUserModels: ''
      },
      sourceList: [],
      sendList: [{label: '微信', value: '1'}, {label: '钉钉', value: '2'}, {label: '微信,钉钉', value: '3'}],
      statusList: [{label: '发布', value: '1'}, {label: '待发布', value: '2'}],
      topicList: [],
      editFlag: false,
      editForm: {},
      queryOptions: [
        {
          columnName: 'sourceType',
          columnValue: '来源类型',
          inputType: 1
        },
        {
          columnName: 'sendingType',
          columnValue: '发送类型',
          selectionData: [
            {
              key: '1',
              label: '微信',
              value: '1'
            },
            {
              key: '2',
              label: '钉钉',
              value: '2'
            },
            {
              key: '3',
              label: '微信,钉钉',
              value: '3'
            }
          ],
          inputType: 2
        },
        {
          columnName: 'releaseType',
          columnValue: '发布状态',
          selectionData: [
            {
              key: '1',
              label: '发布',
              value: '1'
            },
            {
              key: '2',
              label: '待发布',
              value: '2'
            }
          ],
          inputType: 2
        }
      ],
      nameList: [],
      rules: {
        warningUserModels: [
          { required: true, message: '请选择', trigger: 'change' }
        ]
      }
    }
  },
  watch: {

  },
  created() {
    this.getServiceType()
    this.getNameList()
    this.getData()
  },
  methods: {
    getData(resetPage = true) {
      if (resetPage) {
        this.paramsForm.current = 1
      }
      this.isLoading = true
      parameter.getWarnSendConfig(this.paramsForm).then((res) => {
        this.data = res.content
        this.total = res.totalElements
      }).finally(() => {
        this.isLoading = false
      })
    },
    handleSizeChange(val) {
      this.paramsForm.size = val
      this.getData()
    },
    handleCurrentChange(val) {
      this.paramsForm.current = val
      this.getData(false)
    },
    back() {
      this.$emit('back', '0')
    },
    handleClose() {
      this.addForm = {
        sourceType: '',
        sendingType: '',
        releaseType: '2',
        publishTheme: '',
        warningUserModels: ''
      }
      this.addDialogVisible = false
    },
    publish(row) {
      this.$confirm('该操作将发布此预警, 是否继续?', '提示', {
        confirmButtonText: '确定',
        cancelButtonText: '取消',
        type: 'warning'
      })
        .then(() => {
          parameter.publishWarn(row.id).then((res) => {
            this.$notify({
              title: '提示',
              message: '发布成功',
              type: 'success'
            })
            this.crud.refresh()
          })
        })
        .catch(() => {
          this.$message({
            type: 'info',
            message: '已取消发布'
          })
        })
    },
    unpublish(row) {
      this.$confirm('该操作将该状态改为待发布, 是否继续?', '提示', {
        confirmButtonText: '确定',
        cancelButtonText: '取消',
        type: 'warning'
      })
        .then(() => {
          parameter.unpublishWarn(row.id).then((res) => {
            this.$notify({
              title: '提示',
              message: '修改成功',
              type: 'success'
            })
            this.crud.refresh()
          })
        }).catch(() => {
          this.$message({
            type: 'info',
            message: '已取消'
          })
        })
    },
    editClose() {
      this.editFlag = false
      this.editForm = {}
    },
    editData(row) {
      this.editFlag = true
      const obj = JSON.stringify(row)
      this.editForm = JSON.parse(obj)
      const arr = []
      this.editForm.warningUserModels.forEach((item) => {
        arr.push(item.id)
      })
      this.editForm.warningUserModels = arr
    },
    delData(row) {
      this.$confirm('此操作将永久删除, 是否继续?', '提示', {
        confirmButtonText: '确定',
        cancelButtonText: '取消',
        type: 'warning'
      })
        .then(() => {
          parameter.delWarn(row.id).then((res) => {
            this.$notify({
              title: '提示',
              message: '删除成功',
              type: 'success'
            })
            this.crud.refresh()
          })
        })
        .catch(() => {
          this.$message({
            type: 'info',
            message: '已取消删除'
          })
        })
    },
    getServiceType() {
      parameter.getServiceType().then((res) => {
        this.sourceList = res
      })
    },
    getTopic() {
      this.topicList = []
      parameter.getTypeConfig(this.addForm.sourceType).then((res) => {
        res.forEach((item) => {
          item.cmTargetList.forEach((val) => {
            this.topicList.push(val.targetName)
          })
        })
      })
    },
    getNameList() {
      this.nameList = []
      parameter.getNameList().then((res) => {
        res.forEach((item) => {
          const obj = {label: item.recipientRemarks, value: item.id}
          this.nameList.push(obj)
        })
      })
    },
    addSend() {
      this.$refs['form'].validate(valid => {
        if (valid) {
          const arr = []
          this.addForm.warningUserModels.forEach((item) => {
            const obj = {id: item}
            arr.push(obj)
          })
          this.addForm.warningUserModels = arr
          parameter.addWarn(this.addForm).then((res) => {
            this.$notify({
              title: '提示',
              message: '添加成功',
              type: 'success'
            })
            this.getData()
            this.handleClose()
          })
        }
      })
    },
    editSend() {
      this.$refs['forms'].validate(valid => {
        if (valid) {
          const arr = []
          this.editForm.warningUserModels.forEach((item) => {
            const obj = {id: item}
            arr.push(obj)
          })
          this.editForm.warningUserModels = arr
          parameter.editWarn(this.editForm).then((res) => {
            this.$notify({
              title: '提示',
              message: '修改成功',
              type: 'success'
            })
            this.getData()
            this.editClose()
          })
        }
      })
    }
  }
}
</script>
